/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gr.context;

import com.gr.search.Connector;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
/**
 *
 * @author Hieudt
 */
public class QueryLog {
    
    private int userID;
    private java.sql.Date date;
    private String query;
    private int clickedTime = 0;
    private String IPAddr;
    private ArrayList<String> clickedFiles;
    public QueryLog(String q, int id){
        query = q;
        this.userID =id;
        Calendar cal = Calendar.getInstance();
        //SimpleDateFormat sdf = new SimpleDateFormat("dd MMMMM yyyy");
        date = new java.sql.Date(cal.getTime().getTime());
        System.out.println("Now : " + date.toString());
        //clickedFiles=new ArrayList<String>();
    }
    public QueryLog(String q, int id, java.sql.Date date){
        query = q;
        this.userID =id;
        this.date = date;
        //clickedFiles=new ArrayList<String>();
    }
 
    
    //method
    public int getUserID(){
        return this.userID;
    }
    public void setQuery(String q){
        query = q;
        
}
    public void setUserID(int id){
        this.userID=id;
    }
    public void setIPAddr(String ip){
        IPAddr = ip;
    }
    public String getQuery(){
        return this.query;
    }
    public void addClickedFile(String str){
        clickedFiles.add(str);
    
    }
    public void increaseClickedTime(){
        clickedTime++;
    }
    public String queryLogToString(){
        StringBuilder buf = new StringBuilder(query);
        //for(int i=0;i<clickedTime;i++)
            buf.append("(").append(date).append(")");
//            buf.append(clickedFiles.toString());
            buf.append("\n");
        return buf.toString();
    }
    public void resetQueryLog(){
        query = null;
        clickedFiles = null;
        clickedTime = 0;
    }
    public void writeLog(){
     try
            {
                Connection con = Connector.connect2();
                java.sql.PreparedStatement stmt=con.prepareStatement("INSERT INTO log(user_id,query,date) VALUES(?, ?, ?);");
                     stmt.setInt(1,userID);
                     stmt.setString(2,query);
                     //stmt.setString(3,clickedFiles.toString());
                     stmt.setDate(3, date);
                     stmt.executeUpdate();
            }
        catch (ClassNotFoundException ex) {
        }catch(SQLException e)
		{
			 System.err.println("Cant write");
		}
    }
}
